package com.chf.job.controller;

import com.chf.job.common.Const;
import com.chf.job.pojo.*;
import com.chf.job.service.*;
import com.chf.job.vo.ReleaseVo;
import com.chf.job.vo.ResumeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 控制页面跳转
 */
@Controller
public class PageController {
    @Autowired
    private ResumeService resumeService;
    @Autowired
    private HopeJobService hopeJobService;
    @Autowired
    private WorkShowService workShowService;
    @Autowired
    private WorkExperService workExperService;
    @Autowired
    private EducationService educationService;
    @Autowired
    private IOpenService openService;
    @Autowired
    private MailService mailService;
    @Autowired
    private TagService tagService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private ReleaseVoService releaseVoService;
    @Autowired
    private FounderService founderService;
    @Autowired
    private ProductService productService;
    @Autowired
    private DeliverService deliverService;
    @Autowired
    private ResumeVoService resumeVoService;


    /**
     * 跳转到注册页面
     */
    @RequestMapping("page/register.html")
    public String registerPage() {
        return "register";
    }

    /**
     * 跳转到登录界面
     */
    @RequestMapping("page/login.html")
    public String loginPage() {
        return "login";
    }

    /**
     * 跳转到首页
     */
    @RequestMapping("page/index.html")
    public String indexPage(ModelMap modelMap) {
        // 按照倒序查询热门职位
        List<ReleaseVo> hotList = releaseVoService.orderByClickNum();
        modelMap.addAttribute("hotList", hotList);
        // 根据时间查询最新职位
        List<ReleaseVo> newList = releaseVoService.orderByUpdateTime();
        modelMap.addAttribute("newList", newList);
        return "index";
    }

    /**
     * 跳转到公司列表界面
     */
    @RequestMapping("page/companyList.html")
    public String companyListPage() {
        return "companyList";
    }

    /**
     * 跳转到工作详情信息界面
     */
    @RequestMapping("page/jobDetail.html")
    public String jobDetailPage() {
        return "jobDetail";
    }

    /**
     * 未登录情况下跳转到找回密码界面
     */
    @RequestMapping("page/getBackPwd.html")
    public String getBackPwdPage() {
        return "getBackPwd";
    }

    /**
     * 跳转到公司介绍页面
     */
    @RequestMapping("page/companyDetail.html")
    public String companyDetailPage() {
        return "companyDetail";
    }

    /**
     * 跳转到简历界面
     */
    @RequestMapping("page/resume.html")
    public String resumePage(HttpServletRequest request, ModelMap modelMap) {
        // 去简历表数据库查询用户基本信息
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        Resume resume = resumeService.getUserBaseInfo(user.getEmail());
        if (resume == null) {
            resumeService.addResume(user.getEmail());
        }
        Resume resumes = resumeService.getUserBaseInfo(user.getEmail());
        modelMap.addAttribute("resume", resumes);
        // 根据简历id查询期望工作
        HopeJob hopeJob = hopeJobService.getById(resumes.getId());
        modelMap.addAttribute("hopeJob", hopeJob);
        // 根据简历id查询作品展示
        List<WorkShow> workShow = workShowService.getById(resumes.getId());
        modelMap.addAttribute("workShow", workShow);
        // 根据简历id查询工作经历
        List<WorkExper> workExperList = workExperService.getByResumeId(resumes.getId());
        modelMap.addAttribute("workExperList", workExperList);
        // 根据简历id查询教育经历
        List<Education> educationList = educationService.getByResumeId(resumes.getId());
        modelMap.addAttribute("education", educationList);
        return "resume";
    }

    /**
     * 跳转到简历概览界面
     */
    @RequestMapping("page/resumeOverView.html")
    public String resumeOverViewPage(HttpServletRequest request, ModelMap modelMap) {
        // 去简历表数据库查询用户基本信息
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        Resume resume = resumeService.getUserBaseInfo(user.getEmail());
        modelMap.addAttribute("resume", resume);
        // 根据简历id查询期望工作
        HopeJob hopeJob = hopeJobService.getById(resume.getId());
        modelMap.addAttribute("hopeJob", hopeJob);
        // 根据简历id查询作品展示
        List<WorkShow> workShow = workShowService.getById(resume.getId());
        modelMap.addAttribute("workShow", workShow);
        // 根据简历id查询工作经历
        List<WorkExper> workExperList = workExperService.getByResumeId(resume.getId());
        modelMap.addAttribute("workExperList", workExperList);
        // 根据简历id查询教育经历
        List<Education> educationList = educationService.getByResumeId(resume.getId());
        modelMap.addAttribute("education", educationList);
        return "resumeOverView";
    }

    /**
     * 跳转到登录状态下修改密码界面
     */
    @RequestMapping("page/updatePassword.html")
    public String updatePasswordPage() {
        return "updatePassword";
    }

    /**
     * 跳转到开通服务页面
     */
    @RequestMapping("page/openService.html")
    public String openService(HttpServletRequest request, ModelMap modelMap, HttpServletResponse response) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        OpenService open = openService.getByRegisterEmail(user.getEmail());
        // 第一步没有填写
        if (open == null) {
            return "openServiceFirst";
        }

        // 第二步没有填写
        if (StringUtils.isBlank(open.getCompanyName())) {
            return "openServiceSecond";
        }
        // 发送邮件
        if (StringUtils.isBlank(open.getServiceStatus())) {
            boolean result = mailService.sendActiveEmail(user.getEmail(), response);
            if (!result) {
                return null;
            }
        }
        modelMap.addAttribute("activeEmail", user.getEmail());
        return "openServiceThird";
    }

    /**
     * 填写公司信息第一步界面
     */
    @RequestMapping("page/addCompanyFirst.html")
    public String addCompanyFirstPage(HttpServletRequest request, ModelMap modelMap) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        Company company = companyService.getByEmail(user.getEmail());
        OpenService open = openService.getByRegisterEmail(user.getEmail());
        if (open == null) {
            modelMap.addAttribute("companyName", open.getCompanyName());
            return "companyInfoFirst";
        }
        if (company == null) {
            modelMap.addAttribute("companyName", open.getCompanyName());
            return "companyInfoFirst";
        }
        // 判断标签是否填写
        List<Tags> tags = tagService.getTagByCompanyId(company.getId());
        if (CollectionUtils.isEmpty(tags)) {
            return "tags";
        }
        // 创始人团队是否为空
        List<Founder> founders = founderService.getByCompanyId(company.getId());
        if (CollectionUtils.isEmpty(founders)) {
            return "founder";
        }
        // 跳转到公司产品页面
        List<Product> products = productService.getByCompanyId(company.getId());
        if (CollectionUtils.isEmpty(products)) {
            return "product";
        }
        // 跳转到公司详细介绍
        if (StringUtils.isBlank(company.getDetailInrduction())) {
            return "companyIntroduce";
        }
        return "release";
    }

    /**
     * 跳转到发布职位界面
     */
    @RequestMapping("page/releasePost.html")
    public String releasePost(HttpServletRequest request, ModelMap modelMap) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        OpenService byRegisterEmail = openService.getByRegisterEmail(user.getEmail());
        if (byRegisterEmail == null) {
            return "redirect:/page/openService.html";
        }
        Company company = companyService.getByEmail(user.getEmail());
       if ( company == null) {
           return "redirect:/page/addCompanyFirst.html";
       }
        // 未处理简历
        List<Deliver> untreatedResume = deliverService.getByCompanyEmail(user.getEmail());
        modelMap.addAttribute("untreatedResume", untreatedResume);
        // 已通知面试简历个数
        List<Deliver> sendResume = deliverService.getByCompanyEmailAndStatus(user.getEmail());
        modelMap.addAttribute("sendResume", sendResume);
        // 不合适的简历
        List<Deliver> notSuit = deliverService.getByCompanyEmailStatus(user.getEmail());
        modelMap.addAttribute("notSuit", notSuit);
        // 有效职位
        List<ReleaseVo> releaseVo = releaseVoService.getByEmail(user.getEmail());
        modelMap.addAttribute("releaseVo", releaseVo);
        // 无效职位
        List<ReleaseVo> unReleaseVo = releaseVoService.getInvalidPositionByEmail(user.getEmail());
        modelMap.addAttribute("unReleaseVo", unReleaseVo);
        modelMap.addAttribute("company", company);
        return "release";

    }
    /**
     * 跳转到发布成功界面
     */
    @RequestMapping("page/addReleaseSuccess.html")
    public String addReleaseSuccessPage(HttpServletRequest request, ModelMap modelMap) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        Company company = companyService.getByEmail(user.getEmail());
        // 未处理简历
        List<Deliver> untreatedResume = deliverService.getByCompanyEmail(user.getEmail());
        modelMap.addAttribute("untreatedResume", untreatedResume);
        // 已通知面试简历个数
        List<Deliver> sendResume = deliverService.getByCompanyEmailAndStatus(user.getEmail());
        modelMap.addAttribute("sendResume", sendResume);
        // 不合适的简历
        List<Deliver> notSuit = deliverService.getByCompanyEmailStatus(user.getEmail());
        modelMap.addAttribute("notSuit", notSuit);
        // 有效职位
        List<ReleaseVo> releaseVo = releaseVoService.getByEmail(user.getEmail());
        modelMap.addAttribute("releaseVo", releaseVo);
        // 无效职位
        List<ReleaseVo> unReleaseVo = releaseVoService.getInvalidPositionByEmail(user.getEmail());
        modelMap.addAttribute("unReleaseVo", unReleaseVo);
        modelMap.addAttribute("company", company);
        return "addReleaseSuccess";
    }

    /**
     * 跳转到有效职位界面
     */
    @RequestMapping("page/effectivePosition.html")
    public String effectivePositionPage(HttpServletRequest request, ModelMap modelMap) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        List<ReleaseVo> releaseVo = releaseVoService.getByEmail(user.getEmail());
        modelMap.addAttribute("releaseVo", releaseVo);
        // 未处理简历
        List<Deliver> untreatedResume = deliverService.getByCompanyEmail(user.getEmail());
        modelMap.addAttribute("untreatedResume", untreatedResume);
        // 已通知面试简历个数
        List<Deliver> sendResume = deliverService.getByCompanyEmailAndStatus(user.getEmail());
        modelMap.addAttribute("sendResume", sendResume);
        // 不合适的简历
        List<Deliver> notSuit = deliverService.getByCompanyEmailStatus(user.getEmail());
        modelMap.addAttribute("notSuit", notSuit);
        // 无效职位
        List<ReleaseVo> unReleaseVo = releaseVoService.getInvalidPositionByEmail(user.getEmail());
        modelMap.addAttribute("unReleaseVo", unReleaseVo);
        return "effectivePosition";
    }

    /**
     * 跳转到无效职位界面
     */
    @RequestMapping("page/invalidPosition.html")
    public String invalidPositionPage(HttpServletRequest request, ModelMap modelMap) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        List<ReleaseVo> unReleaseVo = releaseVoService.getInvalidPositionByEmail(user.getEmail());
        modelMap.addAttribute("unReleaseVo", unReleaseVo);
        // 未处理简历
        List<Deliver> untreatedResume = deliverService.getByCompanyEmail(user.getEmail());
        modelMap.addAttribute("untreatedResume", untreatedResume);
        // 已通知面试简历个数
        List<Deliver> sendResume = deliverService.getByCompanyEmailAndStatus(user.getEmail());
        modelMap.addAttribute("sendResume", sendResume);
        // 不合适的简历
        List<Deliver> notSuit = deliverService.getByCompanyEmailStatus(user.getEmail());
        modelMap.addAttribute("notSuit", notSuit);
        // 有效职位
        List<ReleaseVo> releaseVo = releaseVoService.getByEmail(user.getEmail());
        modelMap.addAttribute("releaseVo", releaseVo);
        return "invalidPosition";
    }

    /**
     * 跳转到工作详细界面
     */
    @RequestMapping("page/workDetail.html")
    public String workDetailPage(@RequestParam("releaseId") Integer releaseId, ModelMap modelMap) {
        ReleaseVo releaseVo = releaseVoService.getByReleaseId(releaseId);
        modelMap.addAttribute("releaseVo", releaseVo);
        String desc = releaseVo.getPositDesc();
        List<String> descList = com.chf.job.utils.StringUtils.splitString(desc);
        modelMap.addAttribute("descList", descList);
        String require = releaseVo.getPositRequire();
        List<String> requireList = com.chf.job.utils.StringUtils.splitString(require);
        modelMap.addAttribute("requireList", requireList);
        String other = releaseVo.getPositOther();
        List<String> otherList = com.chf.job.utils.StringUtils.splitString(other);
        modelMap.addAttribute("otherList", otherList);
        return "workDetail";
    }
    /**
     * 跳转到未处理简历的界面
     */
    @RequestMapping("page/untreatedResume.html")
    public String untreatedResumePage(HttpServletRequest request, ModelMap modelMap) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        List<Deliver> delivers = deliverService.getByCompanyEmail(user.getEmail());
        modelMap.addAttribute("delivers", delivers);
        // 已通知面试简历个数
        List<Deliver> sendResume = deliverService.getByCompanyEmailAndStatus(user.getEmail());
        modelMap.addAttribute("sendResume", sendResume);
        // 不合适的简历
        List<Deliver> notSuit = deliverService.getByCompanyEmailStatus(user.getEmail());
        modelMap.addAttribute("notSuit", notSuit);
        // 有效职位
        List<ReleaseVo> releaseVo = releaseVoService.getByEmail(user.getEmail());
        modelMap.addAttribute("releaseVo", releaseVo);
        // 无效职位
        List<ReleaseVo> unReleaseVo = releaseVoService.getInvalidPositionByEmail(user.getEmail());
        modelMap.addAttribute("unReleaseVo", unReleaseVo);
        List<ResumeVo> resumes = resumeVoService.getByList(delivers);
        modelMap.addAttribute("resumes", resumes);

        return "untreatedResume";
    }
    /**
     * 跳转到已通知面试简历的界面
     */
    @RequestMapping("page/sendResume.html")
    public String sendResumePage(HttpServletRequest request, ModelMap modelMap) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        List<Deliver> delivers = deliverService.getByCompanyEmailAndStatus(user.getEmail());
        modelMap.addAttribute("delivers", delivers);
        // 未处理简历
        List<Deliver> untreatedResume = deliverService.getByCompanyEmail(user.getEmail());
        modelMap.addAttribute("untreatedResume", untreatedResume);
        // 不合适的简历
        List<Deliver> notSuit = deliverService.getByCompanyEmailStatus(user.getEmail());
        modelMap.addAttribute("notSuit", notSuit);
        // 有效职位
        List<ReleaseVo> releaseVo = releaseVoService.getByEmail(user.getEmail());
        modelMap.addAttribute("releaseVo", releaseVo);
        // 无效职位
        List<ReleaseVo> unReleaseVo = releaseVoService.getInvalidPositionByEmail(user.getEmail());
        modelMap.addAttribute("unReleaseVo", unReleaseVo);

        List<ResumeVo> resumes = resumeVoService.getByList(delivers);
        modelMap.addAttribute("resumes", resumes);
        return "sendResume";
    }

    /**
     * 跳转到不合适的简历
     */
    @RequestMapping("page/unseemlinessResume.html")
    public String unseemlinessResumePage(HttpServletRequest request, ModelMap modelMap) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        List<Deliver> delivers = deliverService.getByCompanyEmailStatus(user.getEmail());
        modelMap.addAttribute("delivers", delivers);
        // 未处理简历
        List<Deliver> untreatedResume = deliverService.getByCompanyEmail(user.getEmail());
        modelMap.addAttribute("untreatedResume", untreatedResume);
        // 已通知面试简历个数
        List<Deliver> sendResume = deliverService.getByCompanyEmailAndStatus(user.getEmail());
        modelMap.addAttribute("sendResume", sendResume);
        // 有效职位
        List<ReleaseVo> releaseVo = releaseVoService.getByEmail(user.getEmail());
        modelMap.addAttribute("releaseVo", releaseVo);
        // 无效职位
        List<ReleaseVo> unReleaseVo = releaseVoService.getInvalidPositionByEmail(user.getEmail());
        modelMap.addAttribute("unReleaseVo", unReleaseVo);

        List<ResumeVo> resumes = resumeVoService.getByList(delivers);
        modelMap.addAttribute("resumes", resumes);
        return "unseemlinessResume";
    }

    /**
     * 跳转到投递者简历概览界面
     */
    @RequestMapping("page/resumeOverViewByEmail.html")
    public String resumeOverViewPage(@RequestParam("email") String email, ModelMap modelMap) {
        // 去简历表数据库查询用户基本信息
        Resume resume = resumeService.getUserBaseInfo(email);
        modelMap.addAttribute("resume", resume);
        // 根据简历id查询期望工作
        HopeJob hopeJob = hopeJobService.getById(resume.getId());
        modelMap.addAttribute("hopeJob", hopeJob);
        // 根据简历id查询作品展示
        List<WorkShow> workShow = workShowService.getById(resume.getId());
        modelMap.addAttribute("workShow", workShow);
        // 根据简历id查询工作经历
        List<WorkExper> workExperList = workExperService.getByResumeId(resume.getId());
        modelMap.addAttribute("workExperList", workExperList);
        // 根据简历id查询教育经历
        List<Education> educationList = educationService.getByResumeId(resume.getId());
        modelMap.addAttribute("education", educationList);
        return "resumeOverView";
    }

    /**
     * 跳转到搜索职位界面
     */
    @RequestMapping("page/posiList.html")
    public String test() {
        return "positList";
    }
}
